/*
Content: Figures 4, E.3, Table E.3
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


***********************************************
** HETEROGENEOUS EFFECTS: Figure 4 and E3 **
***********************************************

use "$final/finaldata_terrorsoep_light_final.dta", clear

*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Gender
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	

eststo clear
forvalues i = 1/2 {

qui eststo gender`i': reghdfe remain_ger_per postterror_m3y_p90  c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_m3y==1  & sex==`i' , vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & sex==`i'  & postterror_m3y_p90==0
scalar b = r(mean)	
local gender`i'_0 : di %6.3f scalar(b)
}

coefplot (gender1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`gender1_0'"))  (gender2, mcolor(black)  mlabels(postterror_m3y_p90=1 "m=`gender2_0'")) , ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid)	 keep(postterror_m3y_p90)  xline(0, lcolor(cranberry)) nooffsets aseq swapnames legend(off) graphregion(color(white)) coeflabels(gender1 = "Male"	gender2= "Female") ysize(3) xsize(5.5) mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))

graph save "Graph" "$graph/het_effects_gender_final.gph", replace
graph export "$graph/het_effects_gender_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_gender_final.eps", as(eps) name("Graph") replace


*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Employment status
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*			

eststo clear
local i=1				
foreach event in terror     {	
foreach var in	m3y  {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distance`event'_`var'_p90,-3,3) & distance`event'_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	

qui eststo employ_`i': reghdfe remain_ger_per post`event'_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_`event'_`var'==1  & employed==1 , vce(cluster clusterterror_m3y_p90) absorb($controls)
local i=`i'+1
sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & employed==1   & postterror_m3y_p90==0
scalar b = r(mean)	
local employ_1_0 : di %6.3f scalar(b)

qui eststo employ_`i':  reghdfe remain_ger_per post`event'_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_`event'_`var'==1  & employed==0, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & employed==0 & postterror_m3y_p90==0
scalar b = r(mean)	
local employ_2_0 : di %6.3f scalar(b)
}
}

coefplot (employ_1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`employ_1_0'"))  (employ_2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`employ_2_0'")), keep(postterror_m3y_p90) ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid) xline(0, lcolor(cranberry))  nooffsets aseq swapnames legend(off) graphregion(color(white))  coeflabels(employ_1 = "Employed" ///
					employ_2= "Not Employed" )	 ysize(3) xsize(5.5) mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))
					
graph save "Graph" "$graph/het_effects_empoystatus_final.gph", replace
graph export "$graph/het_effects_empoystatus_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_empoystatus_final.eps", as(eps) name("Graph") replace


*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Years since migration
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	

eststo clear
forvalues i = 1/4 {

qui eststo ysm_cat_g`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_m3y==1  & ysm_cat==`i' , vce(cluster clusterterror_m3y_p90) absorb($controls)	

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & ysm_cat==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local ysm_cat_g`i'_0 : di %6.3f scalar(b)

}


coefplot (ysm_cat_g1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`ysm_cat_g1_0'"))  (ysm_cat_g2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`ysm_cat_g2_0'")) (ysm_cat_g3, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`ysm_cat_g3_0'")) (ysm_cat_g4, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`ysm_cat_g4_0'")), ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid)	 keep(postterror_m3y_p90)  xline(0, lcolor(cranberry)) nooffsets aseq swapnames legend(off) graphregion(color(white)) coeflabels(ysm_cat_g1 = "0-4 years" 	ysm_cat_g2= "5-9 years" ysm_cat_g3= "10-14 years" ysm_cat_g4= "15 or more years") ysize(3) xsize(5.5) mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))
					
graph save "Graph" "$graph/het_effects_ysm_final.gph", replace
graph export "$graph/het_effects_ysm_final.pdf", as(pdf) name("Graph") replace								
graph export "$graph/het_effects_ysm_final.eps", as(eps) name("Graph") replace								


*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Language skills
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	


global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	

eststo clear
forvalues i = 1/3 {

qui eststo oral_german_g`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M  & oral_german_g==`i' & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & oral_german_g==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local oral_german_g`i'_0 : di %6.3f scalar(b)

}


coefplot (oral_german_g1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`oral_german_g1_0'")) (oral_german_g2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`oral_german_g2_0'"))  (oral_german_g3, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`oral_german_g3_0'")), ///
keep(postterror_m3y_p90)  xline(0, lcolor(cranberry)) ciopts(recast(rspike) color(black)) nooffsets aseq swapnames legend(off) graphregion(color(white)) xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid) coeflabels(oral_german_g1 = "Good Oral German" oral_german_g2= "Not Good Oral Ger." oral_german_g3= "No Information") ysize(3) xsize(5.5) mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))

graph save "Graph" "$graph/het_effects_oralgerman_final.gph", replace
graph export "$graph/het_effects_oralgerman_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_oralgerman_final.eps", as(eps) name("Graph") replace




*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Education after migration
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	


eststo clear
forvalues i = 0/2 {

qui eststo educ_aftm2`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat  if $select_reg_M  & sample_terror_m3y==1 & educ_aftm==`i' & sample_terror_m3y==1 , vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & educ_aftm==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local educ_aftm2`i'_0 : di %6.3f scalar(b)
}


coefplot (educ_aftm20, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`educ_aftm20_0'"))  (educ_aftm21, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`educ_aftm21_0'"))  (educ_aftm22, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`educ_aftm22_0'")), ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid) keep(postterror_m3y_p90) xline(0, lcolor(cranberry)) nooffsets aseq swapnames legend(off) graphregion(color(white)) ysize(3) xsize(5.5) ///
coeflabels(educ_aftm20 = "Lower Sec. or below" educ_aftm21= "Upper Second. Educ." educ_aftm22= "Post-Sec. and Uni") mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))
					
graph save "Graph" "$graph/het_effects_educ_final.gph", replace
graph export "$graph/het_effects_educ_final.pdf", as(pdf) name("Graph") replace							
graph export "$graph/het_effects_educ_final.eps", as(eps) name("Graph") replace							



*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Location of family members
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 		

eststo clear
forvalues i = 1/2 {
qui eststo family_abroad`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M  & family_abroad_4==`i' & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & family_abroad_4==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local family_abroad`i'_0 : di %6.3f scalar(b)
}


coefplot (family_abroad1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`family_abroad1_0'"))  (family_abroad2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`family_abroad2_0'")), ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid) keep(postterror_m3y_p90) xline(0, lcolor(cranberry)) nooffsets aseq swapnames legend(off) graphregion(color(white))  coeflabels(family_abroad1 = "Close Family Abroad" ///
family_abroad2= "No Close Family Abroad" ) ysize(3) xsize(5.5) mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))

graph save "Graph" "$graph/het_effects_core_familyabroad_nomiss_final.gph", replace
graph export "$graph/het_effects_core_familyabroad_nomiss_final.pdf", as(pdf) name("Graph") replace	
graph export "$graph/het_effects_core_familyabroad_nomiss_final.eps", as(eps) name("Graph") replace	

*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* News consumption
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	

eststo clear
forvalues i = 1/5 {
qui eststo newspaper_lang_g`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M  & newspaper_lang_g==`i'  & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & newspaper_lang_g==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local newspaper_lang_g`i'_0 : di %6.3f scalar(b)
}



coefplot  (newspaper_lang_g1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`newspaper_lang_g1_0'")) (newspaper_lang_g2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`newspaper_lang_g2_0'")) (newspaper_lang_g3, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`newspaper_lang_g3_0'")) (newspaper_lang_g4, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`newspaper_lang_g4_0'")) (newspaper_lang_g5, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`newspaper_lang_g5_0'")), ciopts(recast(rspike) color(black))	xscale(r(-0.4(0.2)1.6)) xlabel(-0.4(0.2)1.6, nogrid) ///
keep(postterror_m3y_p90) xline(0, lcolor(cranberry)) nooffsets aseq swapnames legend(off) graphregion(color(white)) ///
coeflabels(newspaper_lang_g1 = "Mainly lang. origin" ///
					newspaper_lang_g2= "About The Same" newspaper_lang_g3= "Mainly German" newspaper_lang_g4= "Does not use" newspaper_lang_g5= "No Information") ysize(3) xsize(5.5)	mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))							
					
graph save "Graph" "$graph/het_effects_fnewspaperlang_interv5_final.gph", replace
graph export "$graph/het_effects_fnewspaperlang_interv5_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_fnewspaperlang_interv5_final.eps", as(eps) name("Graph") replace



*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Willingness to take risks 
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	
eststo clear
forvalues i = 0/1 {
qui eststo risk_averse`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M  & risk_averse==`i'  & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & risk_averse==`i'  & postterror_m3y_p90==0
scalar b = r(mean)	
local  risk_averse`i'_0 : di %6.3f scalar(b)
}


coefplot (risk_averse0, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`risk_averse0_0'"))  (risk_averse1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`risk_averse1_0'")) , ///
keep(postterror_m3y_p90)  ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid)  xline(0, lcolor(cranberry))  nooffsets aseq swapnames legend(off) graphregion(color(white)) ///
coeflabels(risk_averse0 = "Not Risk Averse" risk_averse1= "Risk Averse"  ) ysize(3) xsize(5.5) mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))
	

graph save "Graph" "$graph/het_effects_riskaversion_final.gph", replace
graph export "$graph/het_effects_riskaversion_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_riskaversion_final.eps", as(eps) name("Graph") replace


*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Immigration group 
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 				
eststo clear
forvalues i = 1/3 {
qui eststo immig_g`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M  & immig_g2==`i'  & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 & immig_g2==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local immig_g`i'_0 : di %6.3f scalar(b)
}


coefplot (immig_g1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`immig_g1_0'"))  (immig_g2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`immig_g2_0'")) (immig_g3, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`immig_g3_0'")), ///
keep(postterror_m3y_p90)  ciopts(recast(rspike) color(black))	xscale(r(-0.2(0.1)0.6)) xlabel(-0.2(0.1)0.6, nogrid)  xline(0, lcolor(cranberry))  nooffsets aseq swapnames legend(off) graphregion(color(white)) ///
coeflabels(immig_g1 = "Citizen of EU Country" immig_g2= "Asylum Seeker, Refugee" immig_g3= "Other Immig. Group") ysize(3) xsize(5.5)	mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))	

graph save "Graph" "$graph/het_effects_immig_group_final.gph", replace
graph export "$graph/het_effects_immig_group_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_immig_group_final.eps", as(eps) name("Graph") replace


*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*
* Region of origin
*~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~**~*~*~*~*~*~*~*~*	
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 				
eststo clear
forvalues i = 1/4 {
eststo bigregion`i': reghdfe remain_ger_per postterror_m3y_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M  & region_origin2==`i'  & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)

sum	 remain_ger_per if $select_reg_M & sample_terror_m3y==1 &  region_origin2==`i' & postterror_m3y_p90==0
scalar b = r(mean)	
local bigregion`i'_0 : di %6.3f scalar(b)
}


coefplot bigregion* (bigregion1, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`bigregion1_0'")) (bigregion2, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`bigregion2_0'")) (bigregion3, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`bigregion3_0'")) (bigregion4, mcolor(black) mlabels(postterror_m3y_p90=1 "m=`bigregion4_0'")) , ciopts(recast(rspike) color(black))	xscale(r(-0.4(0.2)1.3)) xlabel(-0.4(0.2)1.3, nogrid) ///
keep(postterror_m3y_p90) xline(0, lcolor(cranberry)) nooffsets aseq swapnames legend(off) graphregion(color(white)) ///
coeflabels(bigregion1 = "EU + UK" bigregion2= "FUSSR + Ex-Yugoslavia"  bigregion3= " Turkey + Arabic Speak." bigregion4= "Other")	ysize(3) xsize(5.5)	mlabcolor(none) addplot(scatter @at @ul, ms(i) mlabel(@mlbl) mlabcolor(black) mlabsize(small))

graph save "Graph" "$graph/het_effects_regionp_final.gph", replace
graph export "$graph/het_effects_regionp_final.pdf", as(pdf) name("Graph") replace
graph export "$graph/het_effects_regionp_final.eps", as(eps) name("Graph") replace

*****************************************************************
**** Table E3: statistical differences *************
*****************************************************************

* Location of family members
cap drop het_var
g het_var=0 if family_abroad_4==1
replace  het_var=1 if family_abroad_4==2
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)


estout , stats(F_diff p_diff) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", replace se scalars("F_diff F-stat" "p_diff P-value")  drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Close Family Abroad" ) nonumber

* Employment status
cap drop het_var
g het_var=0 if employed==0
replace  het_var=1 if employed==1

eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

estout , stats(F_diff p_diff) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat" "p_diff P-value")  drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Employed ")  fragment nonumber


* Willingness to take risks 
cap drop het_var
g het_var=0 if risk_averse==0
replace  het_var=1 if risk_averse==1
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

estout , stats(F_diff p_diff) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat" "p_diff P-value")  drop(*het_var *het_var* _cons) b(3) nogaps  mtitle("Risk Averse" ) fragment nonumber

* Gender
cap drop het_var
g het_var=0 if sex==1
replace  het_var=1 if sex==2

eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

estout , stats(F_diff p_diff) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat" "p_diff P-value")  drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Female") fragment nonumber

* Language skills
cap drop het_var
g het_var=0 if oral_german_g==1
replace  het_var=1 if oral_german_g==2
replace  het_var=2 if oral_german_g==3
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var // HERE 

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#2.het_var

estadd scalar F_diff2 = r(F)
estadd scalar p_diff2 = r(p)

estout , stats(F_diff p_diff F_diff2 p_diff2) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat 1" "p_diff P-value 1" "F_diff2 F-stat 2" "p_diff2 P-value 2") drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Oral German") fragment nonumber


* Immigration group 
cap drop het_var
g het_var=0 if immig_g2==1
replace  het_var=1 if immig_g2==2
replace  het_var=2 if immig_g2==3
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#2.het_var

estadd scalar F_diff2 = r(F)
estadd scalar p_diff2 = r(p)

estout , stats(F_diff p_diff F_diff2 p_diff2) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat 1" "p_diff P-value 1" "F_diff2 F-stat 2" "p_diff2 P-value 2")  drop(*het_var *het_var* _cons) b(3) nogaps  mtitle("Migration Group") fragment nonumber



* Education after migration
cap drop het_var
g het_var=0 if educ_aftm==0
replace  het_var=1 if educ_aftm==1
replace  het_var=2 if educ_aftm==2
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var // HERE 

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#2.het_var

estadd scalar F_diff2 = r(F)
estadd scalar p_diff2 = r(p)

estout , stats(F_diff p_diff F_diff2 p_diff2) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat 1" "p_diff P-value 1" "F_diff2 F-stat 2" "p_diff2 P-value 2") drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Education") fragment nonumber


* Region of origin
cap drop het_var
g het_var=0 if region_origin2==1
replace  het_var=1 if region_origin2==2
replace  het_var=2 if region_origin2==3
replace  het_var=3 if region_origin2==4
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var 

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#2.het_var

estadd scalar F_diff2 = r(F)
estadd scalar p_diff2 = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#3.het_var

estadd scalar F_diff3 = r(F)
estadd scalar p_diff3 = r(p)


estout , stats(F_diff p_diff F_diff2 p_diff2 F_diff3 p_diff3) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat 1" "p_diff P-value 1" "F_diff2 F-stat 2" "p_diff2 P-value 2" "F_diff3 F-stat 3" "p_diff3 P-value 3")  drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Broad Regions") fragment nonumber

* Years since migration	
cap drop het_var
g het_var=0 if ysm_cat==1
replace  het_var=1 if ysm_cat==2
replace  het_var=2 if ysm_cat==3
replace  het_var=3 if ysm_cat==4
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

qui: reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#0.het_var= c.postterror_m3y_p90#1.het_var // HERE 

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#2.het_var

estadd scalar F_diff2 = r(F)
estadd scalar p_diff2 = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#3.het_var

estadd scalar F_diff3 = r(F)
estadd scalar p_diff3 = r(p)


estout , stats(F_diff p_diff F_diff2 p_diff2 F_diff3 p_diff3) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat 1" "p_diff P-value 1" "F_diff2 F-stat 2" "p_diff2 P-value 2" "F_diff3 F-stat 3" "p_diff3 P-value 3")  drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Years Since Migration") fragment nonumber

* News consumption
cap drop het_var
g het_var=0 if newspaper_lang_g==1
replace  het_var=1 if newspaper_lang_g==2
replace  het_var=2 if newspaper_lang_g==3
replace  het_var=3 if newspaper_lang_g==4
replace  het_var=4 if newspaper_lang_g==5
eststo clear
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear#i.het_var i.bula#i.het_var i.month#i.syear#i.het_var"

reghdfe remain_ger_per c.postterror_m3y_p90#i.het_var  (c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child)#i.het_var  if $select_reg_M & sample_terror_m3y==1   , vce(cluster clusterterror_m3y_p90) absorb($controls)		

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#1.het_var

estadd scalar F_diff = r(F)
estadd scalar p_diff = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#2.het_var

estadd scalar F_diff2 = r(F)
estadd scalar p_diff2 = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#3.het_var

estadd scalar F_diff3 = r(F)
estadd scalar p_diff3 = r(p)

margins het_var, dydx(postterror_m3y_p90) noestimcheck
test c.postterror_m3y_p90#1.het_var= c.postterror_m3y_p90#4.het_var

estadd scalar F_diff4 = r(F)
estadd scalar p_diff4 = r(p)

estout , stats(F_diff p_diff F_diff2 p_diff2 F_diff3 p_diff3 F_diff4 p_diff4) keep(*postterror_m3y_p90)
esttab using "$table/diff_heteffects_final.tex", append se scalars("F_diff F-stat 1" "p_diff P-value 1" "F_diff2 F-stat 2" "p_diff2 P-value 2" "F_diff3 F-stat 3" "p_diff3 P-value 3" "F_diff4 F-stat 4" "p_diff4 P-value 4") drop(*het_var *het_var* _cons) b(3) nogaps mtitle("Newspaper Language") fragment nonumber
